package com.whli.autumn.mall.metadata.service.impl;

import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.whli.autumn.core.app.constant.AppConstants;
import com.whli.autumn.core.exception.BusinessException;
import com.whli.autumn.core.extend.service.impl.BaseTreeServiceImpl;
import com.whli.autumn.mall.metadata.domain.dto.productcategory.ProductCategoryQueryDTO;
import com.whli.autumn.mall.metadata.domain.entity.ProductCategory;
import com.whli.autumn.mall.metadata.mapper.ProductCategoryMapper;
import com.whli.autumn.mall.metadata.service.IProductCategoryService;
import com.whli.autumn.mall.metadata.domain.vo.productcategory.ProductCategoryVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>商品分类业务实现</p>
 *
 * @author whli
 * @version 1.0.0
 * @since 2023-04-09 09:32
 */
@Service("productCategoryService")
public class ProductCategoryServiceImpl extends BaseTreeServiceImpl<ProductCategory, ProductCategoryMapper> implements IProductCategoryService {

    @Override
    public List<ProductCategoryVO> listProductCategory(ProductCategoryQueryDTO entity) {
        entity.setIsEnable(AppConstants.YES);
        return super.listAll(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean enable(String id) {
        if (StringUtils.isEmpty(id)) {
            return false;
        }
        int rows = mapper.updateStatus(id, AppConstants.YES);
        return SqlHelper.retBool(rows);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean disable(String id) {
        if (StringUtils.isEmpty(id)) {
            return false;
        }
        int rows = mapper.updateStatus(id, AppConstants.NO);
        return SqlHelper.retBool(rows);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String save(ProductCategory entity) {
        if (StringUtils.isEmpty(entity.getCode())) {
            throw new BusinessException("商品分类编码不能为空");
        }
        this.validOneByCode(entity.getCode());
        return super.save(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String update(ProductCategory entity) {
        if (entity == null || StringUtils.isEmpty(entity.getId()) || StringUtils.isEmpty(entity.getCode())) {
            throw new BusinessException("请选择修改数据");
        }
        ProductCategory temp = this.getByPK(entity.getId());
        if (!temp.getCode().equals(entity.getCode())) {
            this.validOneByCode(entity.getCode());
        }
        return super.update(entity);
    }

    /**
     * <p>验证编码是否存在</p>
     *
     * @author whli
     * @since 2023/4/29 14:32
     * @param code
     * @return
     */
    private void validOneByCode(String code) {
        ProductCategory one = new ProductCategory();
        one.setCode(code);
        one = super.getOne(one);
        if (one != null) {
            throw new BusinessException("商品分类编码【" + code + "】已存在，请重新输入");
        }
    }

}
